package com.hengxinguotong.hxgtwygsp.ui.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.support.AndroidSupportInjection;
import dagger.android.support.HasSupportFragmentInjector;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/1/8 15:46
 */

public abstract class BaseFragment extends RxFragment implements HasSupportFragmentInjector
{
    @Inject
    DispatchingAndroidInjector<Fragment> mChildFragmentInjector;
    private Unbinder mUnbinder;

    @Override
    public void onAttach(Context context)
    {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View mView = inflater.inflate(getLayoutResId(), container, false);
        mUnbinder = ButterKnife.bind(this, mView);
        init();
        return mView;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mUnbinder.unbind();
    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector()
    {
        return mChildFragmentInjector;
    }

    /**
     * 获取布局文件的资源ID
     *
     * @return int
     * @author hjz
     * @since 2019/1/8 16:00
     */
    public abstract int getLayoutResId();

    /**
     * 初始化操作
     *
     * @return void
     * @author hjz
     * @since 2019/1/8 16:00
     */
    public abstract void init();
}
